/*
 * @lc app=leetcode.cn id=189 lang=golang
 *
 * [189] 轮转数组
 */
// 3. 翻转数组
// 1234567
// 7654321
// 5674321
// 5671234
// @lc code=start
func rotate(nums []int, k int) {
	k %= len(nums)
	reverse(nums)
	reverse(nums[:k])
	reverse(nums[k:])
}

func reverse(a []int) {
	for i := 0; i < len(a)/2; i++ {
		a[i], a[len(a)-1-i] = a[len(a)-1-i], a[i]
	}
}

// @lc code=end

// 2. 直接计算
// @lc code=start
func rotate(nums []int, k int) {
	res := make([]int, len(nums))
	for i, v := range nums {
		res[(i+k)%len(nums)] = v
	}
	copy(nums, res)
}

// @lc code=end

// 1-2. 后半截拼前半截
// @lc code=start
func rotate(nums []int, k int) {
	k = k % len(nums)
	pre := nums[:len(nums)-k]
	tail := nums[len(nums)-k:]

	copy(nums, append(tail, pre...))
}

// @lc code=end

// 1-1. 先取后半截 再拼前半截
// @lc code=start
func rotate(nums []int, k int) {
	res := make([]int, len(nums))
	k = k % len(nums)
	x := 0
	for i := len(nums) - k; i < len(nums); i++ {
		res[x] = nums[i]
		x++
	}

	for i := 0; i < len(nums)-k; i++ {
		res[x] = nums[i]
		x++
	}
	copy(nums, res)
}

// @lc code=end

